<%@ page language="java" pageEncoding="UTF-8" import="ll.sms.pojo.*"%>
<%@page import="ll.sms.util.SmsUtil"%>
<%@page import="ll.sms.util.AdminProperty"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>代理注册</title>
		<link rel="stylesheet" type="text/css" href="../ext/ext-all.css" />
		<script type="text/javascript" src="../ext/ext-base.js"></script>
		<script type="text/javascript" src="../ext/ext-all.js"></script>
<style type="text/css">
.settings {
	background: url(../img/add.gif) left top no-repeat !important;
	}
.settings_c {
	background: url(../img/cancle.gif) left top no-repeat !important;
	}
</style>
<%
String currentUserId = SmsUtil.sessionHelper(session,AdminProperty.aid);
//String userType =SmsUtil.sessionHelper(session,AdminProperty.arank);

 %>

	<script type="text/javascript">
	Ext.BLANK_IMAGE_URL = '../images/s.gif';
	Ext.onReady(function(){
		Ext.QuickTips.init();
		var proxystore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({url: '../ajaxjsp/proxycombox.jsp?adminbelong=<%=currentUserId%>'}),
        reader: new Ext.data.JsonReader({
        totalProperty: 'totalCount',
        root: 'result',
        fields :[{name:'aid'},{name:'auname'}]
        }),
        remoteSort: true
        });
        //proxystore.load({params:{start:0,limit:6}});
		   var belongProxy = new Ext.form.ComboBox({
		         store: proxystore,
		         width:250,
		         readOnly:true,
		         mode: 'remote',
		         triggerAction: 'all',
		         valueField: 'aid',
		         displayField: 'auname',
		         minListWidth : 100,
		         fieldLabel: '业务员:',
                 name: 'auname',
                 id:'selectoperator',
		         pageSize : 5
		     });


	var UserRecord = Ext.data.Record.create([
        {name: 'uids', type: 'string'},
        {name: 'uuname', type: 'string'},
        {name: 'upwd', type: 'string'},
        {name: 'ucompany', type: 'string'},
        {name: 'usmsnum', type: 'int'},
        {name: 'uregisttime', type: 'string'},
        {name: 'uavailable', type: 'int'}
    ]);

    	var UserRecord2 = Ext.data.Record.create([
        {name: 'uids', type: 'string'},
        {name: 'uuname', type: 'string'},
        {name: 'upwd', type: 'string'},
        {name: 'ucompany', type: 'string'},
        {name: 'usmsnum', type: 'int'},
        {name: 'uregisttime', type: 'string'},
        {name: 'uavailable', type: 'int'}
    ]);


	//该代理下所有成员
    var store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({url: '../ajaxjsp/operatordo.jsp?flag=behindproxy'}),
        reader: new Ext.data.JsonReader({
        totalProperty: 'totalCount',
        root: 'result'
        },UserRecord),
        remoteSort: true
    });

	//属于某业务员下的所有授权成员
       var store2 = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({url: '../ajaxjsp/operatordo.jsp?flag=behindoperator'}),
        reader: new Ext.data.JsonReader({
        totalProperty: 'totalCount',
        root: 'result'
        },UserRecord2),
        remoteSort: true
    });

   var sm = new Ext.grid.CheckboxSelectionModel();
   sm.handleMouseDown = Ext.emptyFn;

   var sm2 = new Ext.grid.CheckboxSelectionModel();
   sm2.handleMouseDown = Ext.emptyFn;

    var columns = new Ext.grid.ColumnModel([
        new Ext.grid.RowNumberer(),
        sm,
        {header: '用户名', dataIndex: 'uuname'},
        {header: '联系方式', dataIndex: 'uphone'},
        {header: '所属代理', dataIndex: 'ucompany'},
        //{header: '用户等级', dataIndex: 'uavailable'},
        {header: '短信余额', dataIndex: 'usmsnum'}
    ]);

    var columns2 = new Ext.grid.ColumnModel([
        new Ext.grid.RowNumberer(),
        sm2,
        {header: '用户名', dataIndex: 'uuname'},
        {header: '联系方式', dataIndex: 'uphone'},
        {header: '所属代理', dataIndex: 'ucompany'},
        //{header: '用户等级', dataIndex: 'uavailable'},
        {header: '短信余额', dataIndex: 'usmsnum'}
    ]);

       columns.defaultSortable = true;
       columns2.defaultSortable = true;
      var grid2 = new Ext.grid.EditorGridPanel({
        title: '已授权用户列表',
        loadMask: true,
        store: store2,
        cm: columns2,
        height : 400,
        margins: '2 2 2 2',
        sm: sm2,
        //width:'50%',
        columnWidth : .5,
        viewConfig: {
        columnsText:'显示的列',
        sortAscText:'升序',
        sortDescText:'降序',
        forceFit: true
        },
        tbar: new Ext.Toolbar({
        items:[
               {
                     id:'add'
                    ,text:"移除"
                    ,iconCls:'settings_c'
                    ,handler: function(){
                    doGrid(1);
                    }
                },'-','->',{text:'业务员',id:'123'},belongProxy

            ]
        })
    });


    //store2.load({params:{start:0,limit:15}});
	store2.load();
        var grid = new Ext.grid.EditorGridPanel({
        title: '未授权用户列表',
        loadMask: true,
        store: store,
        cm: columns,
        height : 400,
        margins: '2 2 2 2',
        columnWidth : .5,
        sm: sm,
        viewConfig: {
        columnsText:'显示的列',
        sortAscText:'升序',
        sortDescText:'降序',
        forceFit: true
        },
        bbar: new Ext.PagingToolbar({
            pageSize: 15,
            store: store,
            displayInfo: true,
            displayMsg: '显示 {0} 条到 {1} 条记录，一共 {2} 条',
            emptyMsg:'没有记录'

        }),
        tbar: new Ext.Toolbar({
        items:[
               {
                     id:'add'
                    ,text:"添加到授权列表"
                    ,iconCls:'settings'
                    ,handler: function(){
                    doGrid(0);
                    }
                }

            ]
        })
    });

    store.load({params:{start:0,limit:15}});
   // grid.on('rowclick',function (grid,rowIndex,event){
   //  var record = grid.getStore().getAt(rowIndex);
   // });



    var viewport = new Ext.Viewport({
	renderTo:'mt',
	layout:'column',

	items:[
	 grid,
	 grid2
	]
	});

   var doGrid =function (i){
   		var g ;
   		var op;
		if(i==0){
		g = grid;
		op='add'; //为业务员添加成员操作
		}else{
		g = grid2;
		op="remove";//为业务员删除成员操作
		}
		var v = Ext.getCmp('selectoperator').getValue();
		if(Ext.isEmpty(v)){
		alert('在执行操作前你必须选定一个需要操作的业务员！');
		return;
		}
        var rows = g.getSelectionModel().getSelections();
        if (rows.length == 0) {
            Ext.MessageBox.alert('警告', '最少选择一条信息，进行操作!');
        }
        else {

                var delid='';
                    for (var i = 0; i < rows.length; i++) {
                        delid=delid+rows[i].get('uids')+'^';
                    }

                 Ext.Ajax.request({
                  url : '../ajaxjsp/operatordo.jsp',
                  method : 'POST',
                  params : {
                      flag : op,
                      ids : delid,
                      opid : v,
                      opreator :v
                  },

                     success: function(response, options) {
                        //获取响应的json字符串
                       var responseArray = Ext.util.JSON.decode(response.responseText);
                            if(responseArray.success==true){
								grid.getStore().reload({params:{flag:'behindproxy',start:0,limit:15,opreator:v}});
								grid2.getStore().reload({params:{flag:'behindoperator',opreator:v}});
                            }
                            else{
                                Ext.Msg.alert('失败','操作失败'+responseArray.msg);
                            }
                         }
              });

        }
    }//dogrid end

	belongProxy.on("select",function(combobox){
	//alert(combobox.getValue());
	grid.getStore().reload({params:{flag:'behindproxy',start:0,limit:15,opreator:combobox.getValue()}});
	grid2.getStore().reload({params:{flag:'behindoperator',opreator:combobox.getValue()}});
	});


});
	</script>

  </head>

  <body bgcolor="#ffffff">
 <br>
    <div id="mt" align="center"></div>
    <div id= "inp"></div>
    <input id="sel" type="hidden"/>
  </body>
</html>
